<?php

/*
 * This file is part of the official PHP MCP SDK.
 *
 * A collaboration between Symfony and the PHP Foundation.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Mcp;

use Mcp\Server\Builder;
use Mcp\Server\Handler\JsonRpcHandler;
use Mcp\Server\Transport\TransportInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Uid\Uuid;

/**
 * @author Christopher Hertel <mail@christopher-hertel.de>
 * @author Kyrian Obikwelu <koshnawaza@gmail.com>
 */
final class Server
{
    public function __construct(
        private readonly JsonRpcHandler $jsonRpcHandler,
        private readonly LoggerInterface $logger = new NullLogger(),
    ) {
    }

    public static function builder(): Builder
    {
        return new Builder();
    }

    public function connect(TransportInterface $transport): void
    {
        $transport->initialize();

        $this->logger->info('Transport initialized.', [
            'transport' => $transport::class,
        ]);

        $transport->onMessage(function (string $message, ?Uuid $sessionId) use ($transport) {
            try {
                foreach ($this->jsonRpcHandler->process($message, $sessionId) as [$response, $context]) {
                    if (null === $response) {
                        continue;
                    }

                    $transport->send($response, $context);
                }
            } catch (\JsonException $e) {
                $this->logger->error('Failed to encode response to JSON.', [
                    'message' => $message,
                    'exception' => $e,
                ]);
            }
        });

        $transport->onSessionEnd(function (Uuid $sessionId) {
            $this->jsonRpcHandler->destroySession($sessionId);
        });
    }
    /**
     * 获取JSON RPC处理器
     */
    public function getJsonRpcHandler(): Handler
    {
        return $this->jsonRpcHandler;
    }
}
